package com.jt.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public User getUserById(User user) {
        return userMapper.selectById(user);
    }

    @Override
    //MP  以对象的方式操作数据库
    public List<User> getUserByNS(User user) {
        //根据对象不为null的条件充当where条件
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(user);
        return userMapper.selectList(userQueryWrapper);
    }

    @Override
    public List<User> User(String name) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("name", name);
        return userMapper.selectList(userQueryWrapper);
    }

    @Override
    public List<User> User02(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("sex", user.getSex())
                .like("name", user.getName());
        return userMapper.selectList(userQueryWrapper);
    }

    @Override
    public String deleteUser03(User user) {
        //QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(user);
        int delete = userMapper.delete(new QueryWrapper<>(user));
        return (delete > 0) ? ("删除成功！") : ("未查询到该数据");
    }

    @Override
    public String saveUser(User user) {
        int insert = userMapper.insert(user);
        return (insert > 0) ? ("新增入库成功！") : ("新增失败");
    }

    @Override
    public String updateUser(User user) {
        int i = userMapper.updateById(user);
        return (i > 0) ? ("修改成功！") : ("修改失败");
    }

    @Override
    public List<User> select(User user) {
//        boolean idBoo =(user.getId()!=null);
//        boolean nameBoo = StringUtils.hasLength(user.getName());
//        boolean ageBoo =(user.getAge()!=null);
//        boolean sexBoo =StringUtils.hasLength(user.getSex());
//        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//        userQueryWrapper.eq(idBoo, "id", user.getId());
//        userQueryWrapper.eq(nameBoo, "name", user.getName());
//        userQueryWrapper.eq(ageBoo, "age", user.getAge());
//        userQueryWrapper.eq(sexBoo, "sex", user.getSex());
        List<User> users = userMapper.selectList(new QueryWrapper<>(user));
        return users;
        /*前端传入数据 '' 与 null 的区别*/
        //User(id=null, name=null, sex=null, age=66) ----- 66(Integer) ---- Total: 1
        //传入 '' 会拼接空串而不是 null 以至于后期拼接where条件不同，查询结果就不同了
        //User(id=null, name=, sex=, age=66) ---- (String), (String), 66(Integer) ---- Total: 0
    }
}
